// pages/megagame/megagame.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    page: 1,
    dataArr: [],
    canUplod: true, //是否可以上拉加载
    winH:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //获取屏幕高度
    wx.getSystemInfo({
      success: (res) => { // 用这种方法调用，this指向Page
        that.setData({
          winH: res.windowHeight
        });
      }
    })
    that.setData({
      url: util.CompetitionLists + '?wk_competition_id=' + options.kgeId,
    })
    //标题
    wx.setNavigationBarTitle({
      title: options.title
    });
    //获取大赛数据列表
    that.getDetailLists();
  },

  onReachBottom: function () {
    console.log('啦啦啦')
    this.scrolltolower();
  },
  onPullDownRefresh: function() {
    console.log('下拉刷新')
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      page: 1
    });
    this.getDetailLists();
  },

  //上拉加载
  scrolltolower: function() {
    wx: wx.showLoading({
      title: '玩命加载中',
    })
    console.log('上拉加载');
    if (this.data.canUplod) {
      this.getDetailLists();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },
  //点击查看详情
  viewFilmDetail: function(e) {
    var dic = this.data.dataArr[e.currentTarget.dataset.index]
    if (dic.user_permission == 0){
      wx:wx.showToast({
        title: '抱歉，该比赛仅限部分人员参与',
        icon:'none'
      })
    }else{
    //查看作品详情
    var idStr = String(e.currentTarget.dataset.id);
    var titleStr = String(e.currentTarget.dataset.title);
    console.log('查看作品' + idStr);
    wx.navigateTo({
      url: `../knowledge/knowledge?kgeId=${idStr}&title=${titleStr}`
    })
    }
  },
  getDetailLists: function() {
    var that = this;
    var dic = {
      page: that.data.page,
      page_size_count: 12,
    }
    util.publicRequestWithToken(
      that.data.url,
      'GET',
      dic,
      //sucess
      function(data) {
          //成功
          if (that.data.page === 1) {
            //清空数据
            that.setData({
              dataArr: []
            })
          }
          var newArr = that.data.dataArr.concat(data.detail.results)
          if (data.detail.next) {
            that.setData({
              canUplod: true,
              page: that.data.page + 1,
              dataArr: newArr
            })
          } else {
            that.setData({
              canUplod: false,
              page: that.data.page,
              dataArr: newArr
            })
          }
      },
      //fail
      function(error) {},
      function() {
        // complete
        wx.hideLoading()
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();

      }
    );
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})